package com.shift.sdk;

import java.util.ArrayList;
import java.util.List;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import com.shift.ccsdk.CCDetail;
import com.shift.sdk.R;
import com.shift.sdk.dialog.OptionDialog;
import com.shift.sdk.view.RectView;

/**
 * Movieモードのアクティビティクラス.
 *
 * @author SHIFT
 */
public class MovieActivity extends ValidateActivity implements OnClickListener{

    /**
     * 解析オプション.
     */
    public static AnalyzeOption option = new AnalyzeOption();

    /**
     * プレビュー開始・終了フラグ.
     */
    private boolean isStarted = true;

    /**
     * プレビュー.
     */
    private MoviePreview moviePreview;
    
    private Button select,clear;
    
    public TextView count;
    /**
     * 返回成功标识
     */
    public static final int RESULT_OK = 100;
    public static final String RESULT_TAG = "results";
    public static final int REQUEST_CODE = 9;
    public static final String INPUT_NAME_TAG = "input_name";
    
    String input_name;
    
    RectView rectView;
    
    Handler handler = new Handler();

    /**
     * アクティビティ起動時の処理.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        input_name = getIntent().getStringExtra(INPUT_NAME_TAG);
    }

    /**
     * @return
     */
    public MoviePreview getMoviePreview(){
        return moviePreview;
    }

    /**
     * プレビュー開始・終了リスナー.
     *
     * @author SHIFT
     */
    private class PreviewControlListener implements OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {

                // プレビュー開始・終了を制御
                if (isStarted) {
                    moviePreview.stopPreview();
                } else {
                    moviePreview.startPreview();
                }
                isStarted = !isStarted;
            }
            return true;
        }
    }

    /**
     * オプションメニューの設定
     */
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean ret = super.onPrepareOptionsMenu(menu);
        menu.clear();
        menu.add(0, Menu.FIRST, Menu.NONE, "設定").setIcon(android.R.drawable.ic_menu_preferences);
        return ret;
    }

    /**
     * オプションメニュー押下時の処理
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // プレビュー停止
        moviePreview.stopPreview();

        showDialog(1);
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void initView() {
    	 setContentView(R.layout.movie);
    	 select = (Button) findViewById(R.id.select);
    	 select.setOnClickListener(this);
    	 clear =  (Button) findViewById(R.id.clear);
    	 clear.setOnClickListener(this);
    	 
         moviePreview = new MoviePreview();
         SurfaceView cameraImageSurface = (SurfaceView)this.findViewById(R.id.cameraImageSurface);
         rectView = (RectView) findViewById(R.id.rectview);
         count = (TextView) findViewById(R.id.count);
         // ディスプレイサイズに合わせてプレビューサイズを調整
         DisplayMetrics metrics = getResources().getDisplayMetrics();

         moviePreview.setSurfaceView(this,null, cameraImageSurface,
                 (int) (metrics.widthPixels*3/4), (int) (metrics.heightPixels ));

//         cameraImageSurface.setOnTouchListener(new PreviewControlListener());
    }
    
    public void drawRect(List<CCDetail> ddCcDetails,int picWidth,int picHeight){
    	rectView.setDdCcDetails(ddCcDetails);
    	rectView.setPicWidth(picWidth);
    	rectView.setPicHeight(picHeight);
    	rectView.invalidate();
    	/**
    	 * 设置高亮的消失
    	 */
    	handler.postDelayed(new DisappearThread(), RectView.DISAPPEAR_TIME);
    }
    
    /**
	 * 清空
	 * @author funever
	 *
	 */
    class DisappearThread implements Runnable{
		@Override
		public void run() {
			rectView.getDdCcDetails().clear();
			rectView.invalidate();
		}
	}
    
    /**
     * ダイアログ生成.
     */
    @Override
    protected Dialog onCreateDialog(int id) {
    	if(id==0){
    		return super.onCreateDialog(id);
    	}else if(id == 1){
    		Dialog dialog = new OptionDialog(this).create();
    		dialog.setCanceledOnTouchOutside(false);
    		return dialog;
    	}
		return null;
    }

	@Override
	public void onClick(View v) {
		if(v == select){
			if(moviePreview.adapter!=null){
				moviePreview.stopPreview();
				ArrayList<CCInfo> results = (ArrayList<CCInfo>) moviePreview.adapter.getList();
				Intent intent = new Intent();
				intent.putExtra(RESULT_TAG, results);
				intent.putExtra(INPUT_NAME_TAG, input_name);
				setResult(RESULT_OK, intent);
				this.finish();
			}
		}else if(v == clear){
			moviePreview.adapter.clear();
		}
	}
	
	@Override
	public void onBackPressed() {
		this.finish();
	}
}
